package com.guofen.service.sjq.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.SjqSolidliquidProduceFlatmaterialMapper;
import com.guofen.pojo.SjqSolidliquidProduceElectricity;
import com.guofen.pojo.SjqSolidliquidProduceFlatmaterial;
import com.guofen.service.sjq.SjqSolidliquidProduceFlatmaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SjqSolidliquidProduceFlatmaterialServiceimpl implements SjqSolidliquidProduceFlatmaterialService {
    @Autowired
    private SjqSolidliquidProduceFlatmaterialMapper sjqSolidliquidProduceFlatmaterialMapper;

    @Override
    public boolean addForm(String date,String updateTime, Double sandOutput, String operator,String remarks1) {
        SjqSolidliquidProduceFlatmaterial sjqSolidliquidProduceFlatmaterial = new SjqSolidliquidProduceFlatmaterial();
        sjqSolidliquidProduceFlatmaterial.setDate(date);
        sjqSolidliquidProduceFlatmaterial.setUpdateTime(updateTime);
        sjqSolidliquidProduceFlatmaterial.setSandOutput(sandOutput);
        sjqSolidliquidProduceFlatmaterial.setOperator(operator);
        sjqSolidliquidProduceFlatmaterial.setRemarks1(remarks1);
        int result = sjqSolidliquidProduceFlatmaterialMapper.insertSelective(sjqSolidliquidProduceFlatmaterial);
        if (result == 1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public SjqSolidliquidProduceFlatmaterial findForm() {
        return sjqSolidliquidProduceFlatmaterialMapper.getlast();
    }

    @Override
    public boolean update(String inspector, String remarks2) {
        SjqSolidliquidProduceFlatmaterial sjqSolidliquidProduceFlatmaterial = sjqSolidliquidProduceFlatmaterialMapper.getlast();
        if (sjqSolidliquidProduceFlatmaterial.getInspector() == null){
            sjqSolidliquidProduceFlatmaterial.setInspector(inspector);
            sjqSolidliquidProduceFlatmaterial.setRemarks2(remarks2);
            sjqSolidliquidProduceFlatmaterialMapper.updateByPrimaryKeySelective(sjqSolidliquidProduceFlatmaterial);
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<SjqSolidliquidProduceFlatmaterial> list = sjqSolidliquidProduceFlatmaterialMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<SjqSolidliquidProduceFlatmaterial> list = sjqSolidliquidProduceFlatmaterialMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return sjqSolidliquidProduceFlatmaterialMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update_record(SjqSolidliquidProduceFlatmaterial record) {
        return sjqSolidliquidProduceFlatmaterialMapper.updateByPrimaryKeySelective(record);
    }
}
